﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XiamenQudianCoffee.model;

namespace XiamenQudianCoffee
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static bool isMake = true;
        public static double MakingTypes = 0;
        public static string img_path = "";
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        public static string WarnPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Warn";
        public static string BreakPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"BreakDowm";
        public static string Printer = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Printer";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }

                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    IPorPortInfo.Instance.CoffeePortName = porPortInfo.CoffeePortName;
                    IPorPortInfo.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    IPorPortInfo.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    IPorPortInfo.Instance.SugarInit3 = porPortInfo.SugarInit3;
                    WuLiaoCoffeeSel.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    WuLiaoCoffeeSel.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    WuLiaoCoffeeSel.Instance.SugarInit3 = porPortInfo.SugarInit3;
                }
                if (!Directory.Exists(WarnPath))
                {
                    Directory.CreateDirectory(WarnPath);
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                WarnPath = WarnPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                if (!Directory.Exists(BreakPath))
                {
                    Directory.CreateDirectory(BreakPath);
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                if (!Directory.Exists(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                BreakPath = BreakPath + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
                if (!Directory.Exists(Printer))
                {
                    Directory.CreateDirectory(Printer);
                }
                if (!Directory.Exists(Printer + @"\" + DateTime.Now.ToString("yyyy")))
                {
                    Directory.CreateDirectory(Printer + @"\" + DateTime.Now.ToString("yyyy"));
                }
                try
                {
                    Directory.Delete(Printer + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.AddMonths(-1).ToString("MM"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(Printer + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM")))
                {
                    Directory.CreateDirectory(Printer + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM"));
                }
                try
                {
                    Directory.Delete(Printer + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.AddDays(-1).ToString("dd"), true);
                }
                catch (Exception)
                {
                }
                if (!Directory.Exists(Printer + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd")))
                {
                    Directory.CreateDirectory(Printer + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd"));
                }
                Printer = Printer + @"\" + DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MM") + @"\" + DateTime.Now.ToString("dd");
             
            }
            catch (Exception)
            {
                return;
            }
        }
        public static string EmailError()
        {
            try
            {
                string msg = "<style>table{ border: 1px solid black; border-collapse: collapse; }table td{border: 1px solid #000000; padding: 5px 15px;font-size:14px;}</style><table>";
                if (WuLiaoCoffeeSel.Instance.CoffeeBean1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (!WuLiaoCoffeeSel.Instance.CoffeeBean2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>CoffeeBean2</td><td>adequate materials</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Milk1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                //if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol)
                //    msg += "<tr><td style='font-weight:bold;'>Cup2</td><td>adequate materials</td></tr>";
                //else
                //    msg += "<tr><td style='font-weight:bold;'>Cup2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Milk2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>Milk2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose1_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar1</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar1</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose2_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar2</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar2</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose3_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar3</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar3</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose4_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar4</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar4</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose5_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar5</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar5</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                if (WuLiaoCoffeeSel.Instance.Levulose6_Sel_Bol)
                    msg += "<tr><td style='font-weight:bold;'>sugar6</td><td>adequate materials</td></tr>";
                else
                    msg += "<tr><td style='font-weight:bold;'>sugar6</td><td style='color:#FF0000;'>Lack of material</td></tr>";
                //if (!IsMsgError)
                //    msg += "<tr><td style='font-weight:bold;'>Coffee Maker</td><td>Ready</td></tr>";
                //else
                //    msg += "<tr><td style='font-weight:bold;'>Coffee Maker</td><td style='color:#FF0000;'>NotReady</td></tr>";
                msg += "</table>";
                return msg;
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
